<template>
  <d2-container>
    <template slot="header">资金管理-新增</template>
    <el-card shadow="never" class="d2-mb titleline">
     <template slot="header" style="height:66px;">
        <span class="title_x el-button--primary fl"></span>
         <p class="title_l fl">主要信息</p>
     </template>
      <el-form
        class="filter-container"
        :rules="rules"
        ref="FYForm"
        :model="FYForm"
        label-width="120px"
      >
        <el-row :gutter="20">
          <el-col :span="4">
            <el-form-item label="部门名称" prop="department">
              <el-select
                v-model="FYForm.department"
                placeholder="请选择"
                style="width:100%;"
              >
                <el-option
                  v-for="item in selectDepartsList"
                  :key="item.department"
                  :label="item.name"
                  :value="item.department"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
           <!-- <el-col :span="4">
            <el-form-item label="单据ID">
              <el-input v-model="FYForm.invoices_no"></el-input>
            </el-form-item>
          </el-col> -->
           <el-col :span="4">
            <el-form-item label="单据日期" prop="date">
              <el-date-picker
              v-model="FYForm.date"
              type="date"
               value-format="yyyy-MM-dd"
                style="width:140px;"
              placeholder="选择日期">
            </el-date-picker>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="购销类型" prop="direction">
                <el-select v-model="FYForm.direction" clearable class="filter-item" placeholder="请选择">
              <el-option label="销售" value="1"></el-option>
              <el-option label="采购" value="0"></el-option>
            </el-select>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="原币币种" prop="currency">
               <el-select
                v-model="FYForm.currency"
                placeholder="请选择"
                style="width:100%;"
              >
               <el-option
                         v-for="item in selectCurrencyList"
                         :key="item.currency"
                         :label="item.name"
                         :value="item.currency"
                 >
                     <span style="float: left">{{item.name}}</span>
                     <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.currency}}</span> -->
                 </el-option>
              </el-select>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="原币金额" prop="original_price">
             <el-input-number v-model="FYForm.original_price" size="small" :precision="2" :step="1" @input="count1"></el-input-number>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="汇率" prop="exchange_rate">
              <el-input-number v-model="FYForm.exchange_rate" size="small" :precision="2" :step="0.1" @input="count1"></el-input-number>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="本币金额" prop="currency_price">
             <el-input-number v-model="FYForm.currency_price" size="small" :precision="2" :step="0.1" disabled></el-input-number>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="客商" prop="customer">
               <el-select
                v-model="FYForm.customer"
                placeholder="请选择"
                style="width:100%;"
              >
                <el-option
                  v-for="item in customerList"
                  :key="item.customer"
                  :label="item.name"
                  :value="item.customer"
                >
                 <span style="float: left">{{item.name}}</span>
                     <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.customer}}</span> -->
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="期初标识" prop="begin_flag">
               <el-select  clearable v-model="FYForm.begin_flag" class="filter-item" placeholder="请选择">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="0"></el-option>
            </el-select>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="已匹配金额" prop="matched_price">
              <el-input-number v-model="FYForm.matched_price" :precision="2" size="small" :step="0.1" disabled :max="FYForm.currency_price"></el-input-number>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="未匹配金额" prop="unmatched_price">
             <el-input-number v-model="FYForm.unmatched_price" :precision="2" size="small" :step="0.1" disabled :min="0"></el-input-number>
            </el-form-item>
          </el-col>
           <el-col :span="4">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="FYForm.remark"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card shadow="never" class="d2-mb titleline">
     <template slot="header" style="height:66px;">
        <span class="title_x el-button--primary fl"></span>
         <p class="title_l fl">资金分配</p>
     </template>
      <!-- <el-card shadow="never" class="d2-mb"> -->
        <div class="tl mb10">
          <el-button type="default" size="small" @click="addRow">
          <d2-icon name="plus" />
          增行
        </el-button>
        <el-button type="default" size="small" icon="el-icon-delete" @click="delData">
          删行
        </el-button>
         <!-- <el-button type="default" size="small" icon="el-icon-delete" @click="handle_count">
          计算
        </el-button> -->
        </div>
      <el-form label-position="right" status-icon :rules="rules" :model="fygltableData" ref="ruleForm">
       <el-table :data="fygltableData" ref="table" tooltip-effect="dark" border stripe style="width: 100%" :row-class-name="tableRow"
        :cell-style="{ 'text-align': 'center' }"
        :header-cell-style="{ 'text-align': 'center' }" @selection-change='selectRow'>
              <el-table-column type="selection" width="45" align="center"></el-table-column>
              <el-table-column label="序号"  type="index" width="60" align="center"></el-table-column>
              <el-table-column  label="资金类型" align="center" width="180">
                <template slot-scope="scope">
                  <el-form-item :prop="scope.$index + '.cap_type'" :rules="rules.cap_type">
                      <el-select v-model="scope.row.cap_type" placeholder="请选择" style="width:100%;">
                        <el-option
                          v-for="item in cap_typeList"
                          :key="item.cap_type"
                          :label="item.label"
                          :value="item.cap_type"
                        >
                          <span style="float: left">{{item.label}}</span>
                          <!-- <span style="float: right; color: #8492a6; font-size: 13px">{{ item.cap_type}}</span> -->
                        </el-option>
                      </el-select>
                   </el-form-item>
                </template>
              </el-table-column>
               <el-table-column  label="敞口号" align="center" width="145">
                <template slot-scope="scope">
                    <el-form-item :prop="scope.$index + '.exposure'" :rules="rules.exposure" class="dib">
                  <el-input prop="exposure" class="post_name" v-model="scope.row.exposure" style="width:60px;margin-right:5px;"></el-input>
                    </el-form-item>
                  <el-button type="primary" size="mini" plain @click="CKIddia(scope.row)">选择</el-button>
                </template>
              </el-table-column>
               <el-table-column  label="敞口数量" align="center" width="145" sortable>
                <template slot-scope="scope">
                   <el-form-item :prop="scope.$index + '.exp_vol'" :rules="rules.exp_vol">
                  <el-input prop="exp_vol" class="exp_vol" v-model="scope.row.exp_vol" type="number" min="1" @input="countChange2" disabled></el-input>
                   </el-form-item>
                </template>
              </el-table-column>
               <el-table-column  label="品名" align="center" width="145">
                <template slot-scope="scope">
                   <el-form-item :prop="scope.$index + '.commodity_name'" :rules="rules.commodity_name">
                  <el-input prop="commodity_name" class="commodity_name" v-model="scope.row.commodity_name" disabled></el-input>
                   </el-form-item>
                </template>
              </el-table-column>
               <el-table-column label="匹配金额">
                <template slot-scope="scope">
                   <el-form-item :prop="scope.$index + '.alloc_amount'" :rules="rules.alloc_amount">
                  <el-input class="alloc_amount" v-model="scope.row.alloc_amount" @input="countChange1" :readonly="readonly" placeholder="不填按单据数量自动匹配"></el-input>
                   </el-form-item>
                </template>
              </el-table-column>
               <el-table-column  label="备注" align="center">
                <template slot-scope="scope">
                   <el-form-item>
                  <el-input prop="remark" class="post_name" v-model="scope.row.remark"></el-input>
                   </el-form-item>
                </template>
              </el-table-column>
        </el-table>
      </el-form>
    </el-card>
    <el-button type="primary" size="small" style="float:right;margin-left: 10px;" @click="FYglChange('FYForm','ruleForm')">保存</el-button>
    <!-- <el-button type="default" size="small" style="float:right">取消</el-button> -->
     <!-- 敞口选择 -->
     <el-dialog :visible.sync="CKIddialogVisible" title="敞口选择" width="78%">
           <el-form
        class="filter-container"
        ref="CKForm"
        :model="CKForm"
        label-width="110px"
      >
      <el-table-column label="序号"  type="index" width="60" align="center"></el-table-column>
        <el-row :gutter="20">
           <el-col :span="4">
             <el-form-item label="敞口号">
                 <el-input
                   placeholder="请输入内容"
                   v-model="CKForm.exposure_no"
                   clearable>
                 </el-input>
              </el-form-item>
          </el-col>
           <el-col :span="4">
              <el-form-item label="敞口类型">
                 <el-select  clearable v-model="CKForm.exp_type" class="filter-item" placeholder="请选择">
                 <el-option label="逐笔" value="0"></el-option>
                 <el-option label="净敞口" value="1"></el-option>
               </el-select>
              </el-form-item>
            </el-col>
           <el-col :span="4">
             <el-form-item label="品名">
               <el-input placeholder="请输入内容" v-model="CKForm.commodity_name" clearable>
              </el-input>
              </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
                 <el-form-item label="日期">
                <el-col :span="11">
                  <el-date-picker type="date" placeholder="选择日期" v-model="CKForm.startDate" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line tc" :span="2">~</el-col>
                <el-col :span="11">
                  <el-date-picker type="date" placeholder="选择日期" v-model="CKForm.endDate" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                </el-col>
              </el-form-item>
            </el-col> -->
           <el-col :span="2">
              <el-button type="primary" size="small" style='margin-top:3px;' @click="handle_CK">
                <d2-icon name="search"/> 查询
              </el-button>
            </el-col>
        </el-row>
      </el-form>
        <el-table :data="CKIdList" border highlight-current-row @current-change="handleCurrentChange_ta">
          <el-table-column type="index" label="序号"></el-table-column>
          <el-table-column prop="name" label="品名" sortable ></el-table-column>
          <el-table-column prop="exposure_no" label="敞口号" sortable></el-table-column>
          <el-table-column prop="exp_type" label="敞口类型" sortable :formatter="formatterET"></el-table-column>
          <el-table-column prop="direction" label="方向" sortable :formatter="formatterDirection"></el-table-column>
          <el-table-column prop="invoice_vol" label="单据数量" sortable></el-table-column>
          <el-table-column prop="exp_vol" label="敞口数量" sortable></el-table-column>
          <el-table-column prop="price" label="价格" sortable></el-table-column>
          <el-table-column prop="brand" label="品牌" sortable></el-table-column>
        </el-table>
         <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange1"
      :current-page="CKForm.currentPage"
      :page-size="CKForm.pageSize"
      layout="total, prev, pager, next"
      :total="CKForm.totalPage"
    >
    </el-pagination>
        <div slot="footer" class="dialog-footer">
            <el-button @click="CKIddialogVisible = false">取消</el-button>
            <el-button type="primary" @click="CKIdChange">保存</el-button>
        </div>
      </el-dialog>
  </d2-container>
</template>

<script>
import { conExposure, addExposure, conspotreddashed, addspotreddashed, calculate, conspotCap, addspotCap, conCurrency, concustomer, conFeetype } from '../../../../../api/url_lcc';
import { getDepartments } from '../../../../../api/url';
export default {
  name: "add_cost",
//   components: {
//     "trade-name": tradeName
//   },
  data() {
    return {
      readonly:false,
        customerList:[],
        selectDepartsList:[],
        FYForm:{
          department:"",
          date:"",
          direction:"",
          currency:"",
          original_price:"",
          exchange_rate:"",
          currency_price:"",
          customer:"",
          begin_flag:"",
          matched_price:"",
        },
        ruleForm:{
          cap_type:"",
          exposure:"",
          invoice_vol:"",
          alloc_amount:0,
        },
        CKIdList:[],
        fygltableData:[],
        selectCurrencyList: [],
        CKForm: {
        page: 1, //当前页
        pageSize: 20,
        totalPage: 1,
        confirm_status:"1",
        commodity_name: "",
        exp_type: "",
        direction: "",
        deal_no: "",
        base_price_name: "",
        // startDate: "",
        // endDate: "",
        department_name: "",
        exposure_no: "",
        salesman: "",
        depot: "",
        brand: "",
        origin: "",
        depot: "",
        standard: "",
      },
       rules: {
            department: [
              { required: true, message: '请选择部门名称', trigger: 'change' }
            ],
            date:[
              { type: 'string', required: true, message: '请选择日期', trigger: 'change' }
            ],
            direction: [
              { required: true, message: '请选择购销类型', trigger: 'change' }
            ],
            currency: [
              { required: true, message: '请选择币种', trigger: 'change' }
            ],
            original_price: [
              { required: true, message: '请输入原币金额', trigger: 'blur' },
            ],
            exchange_rate: [
             { required: true, message: '请输入汇率', trigger: 'blur' },
            ],
            currency_price: [
             { required: true, message: '请输入本币金额', trigger: 'blur' },
            ],
            customer: [
               { required: true, message: '请选择客商', trigger: 'change' },
            ],
            begin_flag: [
               { required: true, message: '请选择期初标识', trigger: 'change' },
            ],
            matched_price: [
              { required: true, message: '请输入已匹配金额', trigger: 'blur' },
            ],
            unmatched_price: [
              { required: true, message: '请输入未匹配金额', trigger: 'blur' },
            ],
            // remark: [
            //   { required: true, message: '请输入备注', trigger: 'blur' },
            // ],
            cap_type: [
              { required: true, message: '请选择资金类型', trigger: 'change' },
            ],
            exposure: [
              { required: true, message: '请选择敞口号', trigger: 'blur' },
            ],
            exp_vol: [
              { required: true, message: '请输入敞口数量', trigger: 'blur' },
            ],
            alloc_amount: [
              { required: true, message: '请输入匹配金额（注意必须先输入原币金额和汇率才能输入匹配金额！）', trigger: 'blur' },
            ],
          },
      tableData: [],
      selectlistRow: [],
      rowNum: 1,
      CKIddialogVisible: false,
      row: "",
      asd2:"",
      cap_typeList:[
        {
          cap_type:'0',
          label:'货款',
        },
         {
          cap_type:'1',
          label:'资金划拨',
        },
         {
          cap_type:'2',
          label:'利息',
        }
      ]
    };
  },
  created() {
           this.tabledatas = [
               { title: '标题1', text: 's111sssa' },
               { title: '标题2', text: 'ss222ssa' },
           ]
           this.tabledatas.map(i => {
               i.show = false
               return i
           })
  },
  methods: {
    //  部门信息
     //树形结构转换为扁平数组
    treeToList(tree) {
      var queen = [];
      var out = [];
      queen = queen.concat(tree);
      while (queen.length) {
        var first = queen.shift();
        if (first.sub_depart) {
          queen = queen.concat(first.sub_depart);
          delete first["sub_depart"];
        }
        out.push(first);
      }
      return out;
    },
     // 查询部门
    handle_getDepartments() {
      getDepartments()
        .then(res => {
          this.selectDepartsList = [];
          var data1 = res.result;
          var arr = this.treeToList(data1);
          for (var i = 0; i < arr.length; i++) {
            this.selectDepartsList[i] = {
              name: arr[i]["name"],
              department: arr[i]["id"]
            };
          }
        })
        .catch(err => {
          this.$message.error(err.msg);
        });
    },
     //  客商信息
     handle_customerChange(){
       concustomer(this.cusForm).then(res => {
          this.customerList = []
            var data = res.result
            for (var i = 0; i < data.length; i++) {
              this.customerList[i] = {
                  'customer': data[i].id,
                  'name': data[i].name
              }
            }
            console.log(1,data,2,this.customerList)
         }).catch(err =>{
          this.$message.error(err.msg);
        });
    },
   
     // 敞口类型逐笔，净敞口
    formatterET(row, column) {
         if (row.exp_type == "0") {
             return '逐笔';
         } else if (row.exp_type == "1") {
             return '净敞口';
         } 
     },
     //  购销类型
    formatterDirection(row, column) {
         if (row.direction == "0") {
             return '采购';
         } else if (row.direction == "1") {
             return '销售';
         } 
     },
    // 行内编辑
    handleCellDataChange({ rowIndex, key, value, row }) {
      debbuger;
      console.log(rowIndex);
      console.log(key);
      console.log(value);
      console.log(row);
    },

    // 单选
    handleCurrentChange(currentRow, oldCurrentRow) {
      console.log(currentRow);
      console.log(oldCurrentRow);
    },
    // 多选
    handleSelectionChange(selection) {
      console.log(selection);
    },
     // 获取表格选中时的数据
      selectRow (val) {
        this.selectlistRow = val
      },
      // 增加行
      addRow () {
       	  var list = {
          rowNum:this.rowNum,
          // cap_type: "", 
	        // exposure: "",   
	        alloc_amount:0, 
	        remark:""
   		 };
        this.fygltableData.unshift(list)
        this.rowNum += 1;
      },
      // 删除方法
      // 删除选中行
      delData () {
        for (let i = 0; i < this.selectlistRow.length; i++) {
          let val = this.selectlistRow
          val.forEach((val, index) => {
            this.fygltableData.forEach((v, i) => {
              if (val.rowNum === v.rowNum) {
                // i 为选中的索引
                this.fygltableData.splice(i, 1)
              }
            })
          })
        }
        // 删除完数据之后清除勾选框
        this.$refs.table.clearSelection()
      },
      // 本币金额
      count1(){
        // alert(111)
        console.log('ed',this.FYForm.original_price,2,this.FYForm.exchange_rate,3,this.FYForm.original_price*this.FYForm.exchange_rate)
          var original_price = this.FYForm.original_price;
          var exchange_rate = this.FYForm.exchange_rate;
          var currency_price = original_price*exchange_rate
          this.FYForm.currency_price = currency_price
          console.log(111,this.FYForm.currency_price)
          this.readonly=false;
      },
      // 计算
      handle_count(){
         let data = this.fygltableData
         var data2 = ""
        data.forEach(function(item, index) {
           var data2 = item.alloc_amount
           console.log(112122,data2)
           var data2 = data2
        })
        if(data2 == ""){
           this.countChange2()
        }else{
          this.countChange1()
        }
      },
      // 计算匹配金额----修改匹配金额
      countChange1(){
         if(this.FYForm.currency_price == ""){
          this.$message.error("请先输入原币金额和汇率！");
          this.readonly=true;
        }else{
          this.readonly=false;
          let data = this.fygltableData
          var Newdata  = [];
          data.forEach(function(item, index) {
              var data1 = item.alloc_amount
              Newdata.push(Number(data1));
              console.log(90,data1,91,Newdata,92,typeof(data1))
           })
           let params = {
               operation: "FEE",
               "alloc_amount": Newdata,
               "currency_price":this.FYForm.currency_price
           };
        calculate(params).then(res => {
                 var data = res.result
                 this.FYForm = {
                     currency_price: data.currency_price,
                     matched_price: data.matched_price,     
                     unmatched_price: data.unmatched_price, 
                     department: this.FYForm.department,
                     date: this.FYForm.date,
                     customer: this.FYForm.customer,
                     currency: this.FYForm.currency,
                     direction: this.FYForm.direction,
                     virtual: this.FYForm.virtual,
                     original_price: this.FYForm.original_price,
                     begin_flag: this.FYForm.begin_flag,
                     exchange_rate: this.FYForm.exchange_rate,
                     remark: this.FYForm.remark, 
                 }
            }).catch(err =>{
          this.$message.error(err.msg);
        });
        }
        },
      // 计算匹配金额----不修改匹配金额
      countChange2(){
        let params = {
            operation: "FEE",
            fee:{
              currency_price:this.FYForm.currency_price,
              data:this.fygltableData
            } 
        };
        calculate(params).then(res => {
                 var data = res.result
                 console.log(12,data)
                 this.FYForm = {
                     currency_price: data.fee.currency_price,
                     matched_price: data.matched_price,     
                     unmatched_price: data.unmatched_price, 
                     department: this.FYForm.department,
                     date: this.FYForm.date,
                     customer: this.FYForm.customer,
                     currency: this.FYForm.currency,
                     direction: this.FYForm.direction,
                     virtual: this.FYForm.virtual,
                     original_price: this.FYForm.original_price,
                     begin_flag: this.FYForm.begin_flag,
                     exchange_rate: this.FYForm.exchange_rate,
                     remark: this.FYForm.remark, 
                 }
                 console.log(10,data.fee.data)
                 this.fygltableData = data.fee.data
            }).catch(err =>{
          this.$message.error(err.msg);
        });
        },
//       // 新增保存handle_FYglChange
      FYglChange(FYForm,ruleForm){
        console.log('this.fygltableData',this.fygltableData) 
         const rule1 = new Promise((resolve, reject) => {
        this.$refs[FYForm].validate(valid => {
          if (valid) {
            resolve();
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      });
      const rule2 = new Promise((resolve, reject) => {
        this.$refs[ruleForm].validate(valid => {
          if (valid) {
            resolve();
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      });
       Promise.all([rule1, rule2]).then(() => {
          if(this.FYForm.unmatched_price<0){
          this.$message.error("已匹配金额不能大于本币金额！");
        }else{
      let data = this.fygltableData
       var Newdata  = [];
       var asd2 = "";
       var data2 = "";
       data.forEach(function(item, index) {
           var data1 = item.cap_type
         if (data1 == "0") {
             var cap_type = '货款';
         } else if (data1 == "1") {
             var cap_type = '资金划拨';
         } else if (data1 == "2") {
             var cap_type = '利息';
         } else if (data1 == "3") {
             var cap_type = '运输费';
         } else if (data1 == "4") {
             var cap_type = '装卸费';
         } else if (data1 == "5") {
             var cap_type = '仓储费';
         } else if (data1 == "6") {
             var cap_type = '保险费';
         } else if (data1 == "7") {
             var cap_type = '质押费';
         } else if (data1 == "8") {
             var cap_type = '清关费';
         }
           Newdata.push(cap_type);
           var last=JSON.stringify(Newdata);
           var asd = last.replace(/"/g, "");
           var asd1 = asd.replace('[','');
           asd2 = asd1.replace(']','');
        })
        console.log('all资金',asd2)
        //  data.forEach(function(item, index) {
        //    var data2 = item.alloc_amount
        //    console.log(112122,data2)
        //    var data2 = data2
        // })
        // if(data2 == ""){
        //    this.countChange2()
        // }else{
        //   this.countChange1()
        // }
        let params = {
            operation: "ADD",
            spot_cap: {
            department: this.FYForm.department,
            date: this.FYForm.date,
            direction: this.FYForm.direction,
            currency: this.FYForm.currency,
            original_price: this.FYForm.original_price,
            exchange_rate: this.FYForm.exchange_rate,
            currency_price: this.FYForm.currency_price,
            customer: this.FYForm.customer,
            begin_flag: this.FYForm.begin_flag,
            remark: this.FYForm.remark || "",
            matched_price: this.FYForm.matched_price,
            unmatched_price: this.FYForm.unmatched_price,
            cap_type_all: asd2,
          },
            spot_alloc_fee: this.fygltableData,
        };
        addspotCap(params).then(res => {
            this.$message.success(res.result);
            // this.$router.push('/arbitrage_tool/cash_management');
            this.$router.push('/arbitrage_tool/cash_management?table=second');
            this.closeing.closePage(this.$store.getters["d2admin/page/allPage"],this.$store.getters["d2admin/page/currentPage"])
          }).catch(err =>{
          this.$message.error(err.msg);
        });
        }
         });
        },
       //  敞口选择弹框
    CKIddia(index, row) {
      this.CKIddialogVisible = true;
       this.CKForm.page = 1;
       this.CKForm.direction = this.FYForm.direction;
      conExposure(this.CKForm).then((res) => {
          this.CKIdList = [];
          var data = res.result.results;
          this.CKForm.totalPage = res.result.count;
          for (var i = 0; i <= data.length; i++) {
            this.CKIdList[i] = {
              settlement_status: data[i]["settlement_status"],
              confirm_status: data[i]["confirm_status"],
              department_name: data[i]["department_name"],
              red_dashed: data[i]["red_dashed"],
              salesman: data[i]["salesman"],
              depot: data[i]["depot"],
              exp_type: data[i]["exp_type"],
              deal_no: data[i]["deal_no"],
              exposure_no: data[i]["exposure_no"],
              direction: data[i]["direction"],
              customer_name: data[i]["customer_name"],
              customer: data[i]["customer"],
              invoice_vol: data[i]["invoice_vol"],
              exp_vol: data[i]["exp_vol"],
              start_date: data[i]["start_date"],
              end_date: data[i]["end_date"],
              department_name: data[i]["department_name"],
              department: data[i]["department"],
              base_valuation: data[i]["base_valuation"],
              exchange_rate: data[i]["exchange_rate"],
              unit_price: data[i]["unit_price"],
              bwd: data[i]["bwd"],
              base_price: data[i]["base_price"],
              piece_num: data[i]["piece_num"],
              piece_vol: data[i]["piece_vol"],
              category: data[i]["category"],
              base_price_name: data[i]["base_price_name"],
              attribute: data[i].commodity.commodity_spot[0]["attribute"],
              brand: data[i].commodity.commodity_spot[0]["brand"],
              id: data[i].commodity.commodity_spot[0]["id"],
              origin: data[i].commodity.commodity_spot[0]["origin"],
              standard: data[i].commodity.commodity_spot[0]["standard"],
              remark: data[i]["remark"],
              currency_price: data[i]["currency_price"],
              commodity: data[i].commodity["id"], //品名ID
              name: data[i].commodity["name"], //品名
              base_valuation: data[i]["base_valuation"], //估值基准ID
              base_price: data[i]["base_price"], //定价基准ID
              price: data[i]["price"],
              begin_flag: data[i]["begin_flag"],
              pricing_method: data[i]["pricing_method"],
              customer: data[i]["customer"],
              currency: data[i]["currency"],
              virtual: data[i]["virtual"],
            };
            // this.CKIdList[i].exposure_no = this.CKIdList[i].exposure_no;
          }
        }).catch(err =>{
          this.$message.error(err.msg);
        });
       this.index_s=index
    },
    // 查询币种
    handle_currencyChange() {
      let params = {
        name: "",
      };
      conCurrency(params).then((res) => {
          this.selectCurrencyList = [];
          var data1 = res.result;
          console.log(902);
          for (var i = 0; i < data1.length; i++) {
            this.selectCurrencyList[i] = {
              currency: data1[i]["id"],
              name: data1[i]["name"],
            };
          }
        }).catch(err =>{
          this.$message.error(err.msg);
        });
    },
    // 敞口选择查询
    // 每页数
    handleSizeChange(val) {
      this.CKForm.pageSize = val;
      this.CKForm.page = 1;
      this.FYGLChange();
    },
    // 当前页
    handleCurrentChange1(val) {
      this.CKForm.page = val;
      this.FYGLChange();
    },
    // 查询
     handle_CK(){
      this.CKForm.page = 1;
      conExposure(this.CKForm).then((res) => {
          this.CKIdList = [];
          this.CKForm.direction = this.FYForm.direction;
          var data = res.result.results;
          this.CKForm.totalPage = res.result.count;
          for (var i = 0; i <= data.length; i++) {
            this.CKIdList[i] = {
              settlement_status: data[i]["settlement_status"],
              confirm_status: data[i]["confirm_status"],
              department_name: data[i]["department_name"],
              red_dashed: data[i]["red_dashed"],
              salesman: data[i]["salesman"],
              depot: data[i]["depot"],
              exp_type: data[i]["exp_type"],
              deal_no: data[i]["deal_no"],
              exposure_no: data[i]["exposure_no"],
              direction: data[i]["direction"],
              customer_name: data[i]["customer_name"],
              customer: data[i]["customer"],
              invoice_vol: data[i]["invoice_vol"],
              exp_vol: data[i]["exp_vol"],
              start_date: data[i]["start_date"],
              end_date: data[i]["end_date"],
              department_name: data[i]["department_name"],
              department: data[i]["department"],
              base_valuation: data[i]["base_valuation"],
              exchange_rate: data[i]["exchange_rate"],
              unit_price: data[i]["unit_price"],
              bwd: data[i]["bwd"],
              base_price: data[i]["base_price"],
              piece_num: data[i]["piece_num"],
              piece_vol: data[i]["piece_vol"],
              category: data[i]["category"],
              base_price_name: data[i]["base_price_name"],
              attribute: data[i].commodity.commodity_spot[0]["attribute"],
              brand: data[i].commodity.commodity_spot[0]["brand"],
              id: data[i].commodity.commodity_spot[0]["id"],
              origin: data[i].commodity.commodity_spot[0]["origin"],
              standard: data[i].commodity.commodity_spot[0]["standard"],
              remark: data[i]["remark"],
              currency_price: data[i]["currency_price"],
              commodity: data[i].commodity["id"], //品名ID
              name: data[i].commodity["name"], //品名
              base_valuation: data[i]["base_valuation"], //估值基准ID
              base_price: data[i]["base_price"], //定价基准ID
              price: data[i]["price"],
              begin_flag: data[i]["begin_flag"],
              pricing_method: data[i]["pricing_method"],
              customer: data[i]["customer"],
              currency: data[i]["currency"],
              virtual: data[i]["virtual"],
            };
          }
        }).catch(err =>{
          this.$message.error(err.msg);
        });
       this.index_s=index
    },
    FYGLChange() {
      conExposure(this.CKForm).then((res) => {
          this.CKIdList = [];
          var data = res.result.results;
          this.CKForm.totalPage = res.result.count;
          for (var i = 0; i <= data.length; i++) {
            this.CKIdList[i] = {
              settlement_status: data[i]["settlement_status"],
              confirm_status: data[i]["confirm_status"],
              department_name: data[i]["department_name"],
              red_dashed: data[i]["red_dashed"],
              salesman: data[i]["salesman"],
              depot: data[i]["depot"],
              exp_type: data[i]["exp_type"],
              deal_no: data[i]["deal_no"],
              exposure_no: data[i]["exposure_no"],
              direction: data[i]["direction"],
              customer_name: data[i]["customer_name"],
              customer: data[i]["customer"],
              invoice_vol: data[i]["invoice_vol"],
              exp_vol: data[i]["exp_vol"],
              start_date: data[i]["start_date"],
              end_date: data[i]["end_date"],
              department_name: data[i]["department_name"],
              department: data[i]["department"],
              base_valuation: data[i]["base_valuation"],
              exchange_rate: data[i]["exchange_rate"],
              unit_price: data[i]["unit_price"],
              bwd: data[i]["bwd"],
              base_price: data[i]["base_price"],
              piece_num: data[i]["piece_num"],
              piece_vol: data[i]["piece_vol"],
              category: data[i]["category"],
              base_price_name: data[i]["base_price_name"],
              attribute: data[i].commodity.commodity_spot[0]["attribute"],
              brand: data[i].commodity.commodity_spot[0]["brand"],
              id: data[i].commodity.commodity_spot[0]["id"],
              origin: data[i].commodity.commodity_spot[0]["origin"],
              standard: data[i].commodity.commodity_spot[0]["standard"],
              remark: data[i]["remark"],
              currency_price: data[i]["currency_price"],
              commodity: data[i].commodity["id"], //品名ID
              name: data[i].commodity["name"], //品名
              base_valuation: data[i]["base_valuation"], //估值基准ID
              base_price: data[i]["base_price"], //定价基准ID
              price: data[i]["price"],
              begin_flag: data[i]["begin_flag"],
              pricing_method: data[i]["pricing_method"],
              customer: data[i]["customer"],
              currency: data[i]["currency"],
              virtual: data[i]["virtual"],
            };
          }
        }).catch(err =>{
          this.$message.error(err.msg);
        });
    },
    // 单选
    handleCurrentChange_ta(val) {
       this.currentRow = val;
     },
     // 保存敞口
    CKIdChange(index) {
      this.CKIddialogVisible = false;
      console.log('保存敞口',this.currentRow);
      console.log( 'index',this.fygltableData,12,this.index_s)
      this.fygltableData[this.index_s.index].exposure=this.currentRow.exposure_no
      this.fygltableData[this.index_s.index].invoice_vol=this.currentRow.invoice_vol
      this.fygltableData[this.index_s.index].exp_vol=this.currentRow.exp_vol  
      this.fygltableData[this.index_s.index].commodity_name=this.currentRow.name
      this.dialogVisible_one = false;
      this.countChange2();
    },
    tableRow({row,rowIndex}){
      row.index=rowIndex
    },
      
  },
  mounted(){
    this.handle_customerChange();
    this.handle_currencyChange();
    this.handle_getDepartments();
    // this.handlefee_typeChange();
    // this.count1();
  }
};
</script>
